library(tidyverse)

vf <- read_tsv("data/voter_data/panelist_identities.tsv", 
               col_types = cols_only(
                   twProfileID = col_character(),
                   tsmart_partisan_score = col_number()
               )) |> 
    mutate(
        party = case_when(
            tsmart_partisan_score < 35 ~ "Unlikely Democrat",
            tsmart_partisan_score > 65 ~ "Likely Democrat",
            (tsmart_partisan_score >= 35) & (tsmart_partisan_score <= 65)  ~ "Uncertain",
            .default = NA_character_
    ))
user_df <- read_tsv("data/raw/user_reference_table.tsv", col_types = cols(userid = col_character()))
user_df <- inner_join(user_df, vf, by = c("userid" = "twProfileID"))

p <- user_df |> 
    select(userid, 
        Domain=domain_score_continuous_political, 
        URL=url_score_continuous_political, 
        party) |> 
    pivot_longer(Domain:URL) |>
    ggplot(aes(x=value, color=party)) + 
        geom_density() + 
        facet_grid(cols = vars(name), scale="free") + 
        theme_classic()+
        theme(text = element_text(family = "serif", size = 16),
            strip.text = element_text(face = "bold"),
            plot.title = element_text(face = "bold", size = 20)) +
        scale_color_manual(values = c("dodgerblue", "gray50", "firebrick")) + 
        labs(x = "Audience Score (Twitter)",
            y = "Users",
            color = "Modeled Partisanship") + 
        theme(panel.border=element_rect(color = "black", fill = NA))
ggsave("results/fig_3.pdf", p, width=10, height=5, dpi = 450)
ggsave("results/score_by_party.pdf", p, width=10, height=5, dpi = 450)
